﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace SplenetiX
{
    class MenuSolo
    {
        public string clickedLink;
        public bool isbacktomainmenu;

        Cursor cursor;
        Background Window;
        MenuSoloButton backtomainmenu, newgame, loadlastgame;
        List<MenuSoloButton> listButton = new List<MenuSoloButton>();

        public MenuSolo()
        {
            clickedLink = "None";
            isbacktomainmenu = true;
        }

        public string ClickedLink
        {
            get { return clickedLink; }
            set { this.clickedLink = value; }
        }
        public bool isBackToMainMenu
        {
            get { return isbacktomainmenu; }
            set { this.isbacktomainmenu = value; }
        }

        public void Load(ContentManager content)
        {
            Window = new Background(new Vector2(0, -10));
            Window.LoadContent(content, "Menu_Interface//Menu_Solo//bgMenuSolo");

            cursor = new Cursor();
            cursor.LoadCursor(content);

            backtomainmenu = new MenuSoloButton("backtomainmenu", new Vector2(519, 508));
            newgame = new MenuSoloButton("newgame", new Vector2(281, 164));
            loadlastgame = new MenuSoloButton("loadlastgame", new Vector2(281, 290));
            listButton.Add(backtomainmenu);
            listButton.Add(newgame);
            listButton.Add(loadlastgame);

            foreach (MenuSoloButton b in listButton)
            {
                b.Load(content, "Menu_Interface//Menu_Solo//" + b.Name);
            }

        }

        public void Update(GameTime gameTime, ContentManager content, GraphicsDevice graphics, bool returnInMenu, Menu menu)
        {
            
            cursor.UpdateCursor(graphics, false);

            foreach (MenuSoloButton b in listButton)
            {
                b.Update(gameTime, content);

                if (b.IsMouseOver)
                {
                    b.Load(content, "Menu_Interface//Menu_Solo//" + b.Name + "_over");
                    cursor.UpdateCursor(graphics, true);
                }
                else
                {
                    b.Load(content, "Menu_Interface//Menu_Solo//" + b.Name);
                    
                }

                if (b.IsClicked && returnInMenu)
                {
                    clickedLink = b.Name;
                    
                    if (clickedLink == "backtomainmenu")
                    {
                        isbacktomainmenu = true;
                        menu.Reset();
     
                    }
             

                }

                
                
            }

            if (!returnInMenu)
                isbacktomainmenu = false;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            
            backtomainmenu.Draw(spriteBatch);

            Window.DrawBackground(spriteBatch);

            foreach (MenuSoloButton b in listButton)
            {
                if (b.Name != "backtomainmenu") 
                b.Draw(spriteBatch);
            }

            cursor.DrawCursor(spriteBatch);

           
        }

    }
    class MenuSoloButton
    {
        string NameButton;
        Vector2 Pos;
        Texture2D texture_button;
        MouseState state;
        bool isMouseOver, isClicked;
        float timer = 0f;

        public MenuSoloButton(string name, Vector2 position)
        {
            NameButton = name;
            Pos = position;

        }

        public string Name
        {
            get { return NameButton; }
            set { this.NameButton = value; }
        }

        public bool IsMouseOver
        {
            get { return isMouseOver; }
            set { this.isMouseOver = value; }
        }

        public bool IsClicked
        {
            get { return isClicked; }
            set { this.isClicked = value; }
        }

        public Vector2 Position
        {
            get { return Pos; }
            set { this.Pos = value; }
        }

        public void Load(ContentManager contentManager, string assetName)
        {
            texture_button = contentManager.Load<Texture2D>(assetName);
        }

        public void Update(GameTime gameTime, ContentManager content)
        {
            state = new MouseState();
            state = Mouse.GetState();

            if (state.X >= Position.X && state.X <= Position.X + texture_button.Width && state.Y >= Position.Y &&
                state.Y <= Position.Y + texture_button.Height)
                isMouseOver = true;

            else isMouseOver = false;


            if (isMouseOver && state.LeftButton == ButtonState.Pressed)
                isClicked = true;

            else isClicked = false;

        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture_button, Position, Color.White);
        }



    }
}
